package cn.edu.xmu.oomall.customer.dao.bo;

import cn.edu.xmu.javaee.core.aop.CopyFrom;
import cn.edu.xmu.javaee.core.model.bo.OOMallObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.customer.controller.dto.AddressDto;
import cn.edu.xmu.oomall.customer.dao.AddressDao;
import cn.edu.xmu.oomall.customer.mapper.openfeign.Po.Region;
import cn.edu.xmu.oomall.customer.mapper.po.AddressPo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 谢传艳
 * @date 2024/12/18
 */
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@ToString(callSuper = true, doNotUseGetters = true)
@CopyFrom({AddressPo.class, AddressDto.class})
public class Address extends OOMallObject implements Serializable {
    // 非默认地址
    public static final Byte NOT_DEFAULT = 0;
    // 默认地址
    public static final Byte DEFAULT = 1;
    //地址上限
    public static final Integer ADDRESS_MAX = 5;

    private Long customerId;
    private Long regionId;
    private String address;
    private String consignee;
    private String mobile;
    private Byte beDefault;

    private Region region;

    public static final Map<Byte,String> BEDEFAULTNAMES = new HashMap<>(){
        {
            put(NOT_DEFAULT, "非默认地址");
            put(DEFAULT, "默认地址");
        }
    };

    @ToString.Exclude
    @JsonIgnore
    @Setter
    private AddressDao addressDao;

    public void cancelDefault(UserDto user) {
        this.beDefault = NOT_DEFAULT;
        this.setModifier(user);
        this.setGmtModified(LocalDateTime.now());
        addressDao.save(this);
    }

    public void setDefault(UserDto user) {
        this.beDefault = DEFAULT;
        this.setModifier(user);
        this.setGmtModified(LocalDateTime.now());
        addressDao.save(this);
    }

    public void modifyAddress(Address address, UserDto user) {
        this.setAddress(address.getAddress());
        this.setConsignee(address.getConsignee());
        this.setMobile(address.getMobile());
        this.setRegionId(address.getRegionId());
        this.setModifier(user);
        this.setGmtModified(LocalDateTime.now());
        addressDao.save(this);
    }
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public Long getRegionId() {
        return regionId;
    }

    public void setRegionId(Long regionId) {
        this.regionId = regionId;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Byte getBeDefault() {
        return beDefault;
    }

    public void setBeDefault(Byte beDefault) {
        this.beDefault = beDefault;
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public Long getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(Long creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {
        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public Long getModifierId() {
        return modifierId;
    }

    public void setModifierId(Long modifierId) {
        this.modifierId = modifierId;
    }

    public String getModifierName() {
        return modifierName;
    }

    public void setModifierName(String modifierName) {
        this.modifierName = modifierName;
    }

    public LocalDateTime getGmtCreate() {
        return gmtCreate;
    }

    public void setGmtCreate(LocalDateTime gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    public LocalDateTime getGmtModified() {
        return gmtModified;
    }

    public void setGmtModified(LocalDateTime gmtModified) {
        this.gmtModified = gmtModified;
    }
}
